package com.mapfinal.server.auth;

import com.jfinal.aop.Before;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.lambkit.common.ResultKit;
import com.lambkit.db.mgr.MgrTable;
import com.lambkit.db.sql.condition.ConditionBuilder;
import com.lambkit.db.sql.condition.SqlBuilder;
import com.mapfinal.server.auth.model.AuthOrganization;
import com.mapfinal.server.auth.web.AuthOrganizationValidator;
import com.mapfinal.server.web.MapfinalServerController;

public class OrganizationController extends MapfinalServerController {

	/**
	 * 数据详细页
	 */
	public void index() {
		MgrTable tbc = getTableView(AuthOrganization.service().getTableName(), true);
		Integer id = getParaToInt(0) == null ? getParaToInt("id") : getParaToInt(0);
		keepTable(tbc, AuthOrganization.service().findById(id));
		render("index.html");
	}
	
	/**
	 * 单条数据
	 */
	public void get() {
		getTableView(AuthOrganization.service().getTableName(), true);
		Integer id = getParaToInt(0) == null ? getParaToInt("id") : getParaToInt(0);
		AuthOrganization datas = AuthOrganization.service().findById(id);
		if(datas==null) {
			renderJson(ResultKit.json(0, "fail", null));
		} else {
			renderJson(ResultKit.json(1, "success", datas));
		}
	}
	
	/**
	 * 数据列表页
	 */
	public void list() {
		getTableDefault(AuthOrganization.service().getTableName(), true);
		render("list.html");
	}
	
	/**
	 * 分页
	 */
	public void page() {
		MgrTable tbc = getTableView(AuthOrganization.service().getTableName(), true);
		if (tbc == null) {
			renderJson(ResultKit.json(0, "fail", "table config is null"));
			return;
		}
		ConditionBuilder cb = getConditionsSQL(tbc).build("");
		SqlBuilder sb = new SqlBuilder();
		String select = sb.appendSelect(tbc).build();
		sb.clear().appendFrom(tbc).appendConditions(cb);
		//排序
		String orderby = getPara("orderby", "id");
		if(StrKit.notBlank(orderby)) sb.appendOrderBy(orderby);
		String sql = sb.build();
		Integer pNumber = getParaToInt("pageNum", getParaToInt("page", 1));
		Integer pSize =  getParaToInt("numPerPage", getParaToInt("limit", 15));
		Page<Record> page = Db.paginate(pNumber, pSize, select, sql, cb.getSqlParas());
		String type = getPara("rt", "default");
		if("default".equalsIgnoreCase(type)) {
			renderJson(ResultKit.json(1, "success", page));
		} else {
			renderJson(ResultKit.page(type, 1, "success", page.getTotalRow(), page.getList()));
		}
		
	}
	
	public void add() {
		render("add.html");
	}
	
	public void edit() {
		getTableView(AuthOrganization.service().getTableName(), true);
		Integer id = getParaToInt(0) == null ? getParaToInt("id") : getParaToInt(0);
		setAttr("model", AuthOrganization.service().findById(id));
		render("add.html");
	}
	
	@Before(AuthOrganizationValidator.class)
	public void save() {
		AuthOrganization env = getModel(AuthOrganization.class, "model");
		boolean flag = env.save();
		if(flag) {
			renderJson(ResultKit.json(1, "success", env));
		} else {
			renderJson(ResultKit.json(0, "fail", "失败"));
		}
	}
	
	@Before(AuthOrganizationValidator.class)
	public void update() {
		AuthOrganization env = getModel(AuthOrganization.class, "model");
		boolean flag = env.update();
		if(flag) {
			renderJson(ResultKit.json(1, "success", env));
		} else {
			renderJson(ResultKit.json(0, "fail", "失败"));
		}
	}
	
	public void delete() {
		boolean flag = false;
		Integer id = getParaToInt(0) == null ? getParaToInt("id") : getParaToInt(0);
		AuthOrganization datas = AuthOrganization.service().findById(id);
		if(datas!=null) {
			flag = datas.delete();
		}
		if(flag) {
			renderJson(ResultKit.json(1, "success", "deleted"));
		} else {
			renderJson(ResultKit.json(0, "fail", "deleted"));
		}
	}
}
